import CollisionHelper from "../tools/CollisionHelper";

export default class Bound  extends cc.Rect{


    // private x: number = 0;

    // private y: number = 0;

    private cx: number = 0;

    private cy: number = 0;

    // private width: number = 0;
    // private height: number = 0;

    private left:number = 0;

    private right:number = 0;

    private bottom:number = 0;

    private top:number = 0;

    private quadrantList:cc.Rect[] = []
    constructor(x: number, y: number, width: number, height: number) {
        super(x,y,width,height)
        this.cx = this.x + this.width / 2;
        this.cy = this.y + this.height / 2;
        let hw = this.width /2;
        let hh = this.height / 2;
        this.quadrantList[0] = cc.rect(this.cx,this.cy,hw,hh);
        this.quadrantList[1] = cc.rect(this.x,this.cy,hw,hh);
        this.quadrantList[2] = cc.rect(this.x,this.y,hw,hh);
        this.quadrantList[3] = cc.rect(this.cx,this.y,hw,hh);

    }

    getQuadrantList():cc.Rect[]{
        return this.quadrantList;
    }
    /**
     * 与哪个象限相交
     * @param rect 
     * @param who 
     */
    isCollide(rect:cc.Rect,who:number){
        if(CollisionHelper.isRectCollideRect(rect,this.quadrantList[who])){
            return who ; 
        }   
        return -1;
    }

    /**
     * 是否在区域内
     * @param rect 
     * @param bounds 
     */
    isInBound(rect: cc.Rect) {
        return CollisionHelper.isRectInRect(rect,this)
    }    

    /**
     * 获得象限的索引 如果是-1 代表跨象限或者离开屏幕。
     * @param rect 
     */
    getIndex(rect: cc.Rect):number{
        for (let index = 0; index < this.quadrantList.length; index++) {
            const element = this.quadrantList[index];
            if(CollisionHelper.isRectInRect(rect,element)){
                return index;
            }  
        }
        return -1;
    }

}
